package com.example.administrator.yuejian_2.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.yuejian_2.R;
import com.example.administrator.yuejian_2.bean.ActivityCancelFollowBean;
import com.example.administrator.yuejian_2.bean.ActivityDetailBean;
import com.example.administrator.yuejian_2.bean.ActivityFollowBean;
import com.example.administrator.yuejian_2.bean.ActivityIsFollowedBean;
import com.example.administrator.yuejian_2.interservice.ActivityService;
import com.example.administrator.yuejian_2.interservice.Const;
import com.example.administrator.yuejian_2.interservice.FollowService;
import com.squareup.picasso.Picasso;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class AcDetailActivity extends AppCompatActivity {

    private ImageButton backButton;
    private ImageButton collectButton;
    private TextView acTitle;
    private ImageView acPic;
    private TextView acTime;
    private TextView acAddress;
    private TextView acCost;
    private TextView acLimit;
    private TextView acIntro;
    private Button enterButton;
    private int aid;
    private int uid;
    private int cost;
    private String name;
    private Context context;
    private int sta = 0;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_acdetail);

        SharedPreferences sharedPreferences = getSharedPreferences("userInfo",
                Context.MODE_PRIVATE);
        uid = sharedPreferences.getInt("user_id",0);

        aid = getIntent().getIntExtra("aid",-1);
        cost = getIntent().getIntExtra("cost",-1);
        name = getIntent().getStringExtra("activityName");
        acTitle = (TextView) findViewById(R.id.textView_acdetitle);
        acTime = (TextView) findViewById(R.id.textView3);
        acAddress = (TextView) findViewById(R.id.textView14);
        acCost = (TextView) findViewById(R.id.textView8);
        acLimit = (TextView) findViewById(R.id.textView10);
        acIntro = (TextView) findViewById(R.id.textView12);
         acPic = (ImageView) findViewById(R.id.imageView);


        backButton= (ImageButton) findViewById(R.id.imageButton3);
        backButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        getdetail();


        enterButton = (Button) findViewById(R.id.button);
        enterButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context,AcEnterActivity.class);
                intent.putExtra("aid",aid);
                intent.putExtra("cost",cost);
                intent.putExtra("name",name);
                context.startActivity(intent);
            }
        });

        isfollowed();
        collectButton = (ImageButton) findViewById(R.id.imageButton4);
        collectButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (sta == 0){
                    acfollow();
                }else {
                    accancelfollow();
                }
            }
        });

    }

    private void getdetail(){
        context = AcDetailActivity.this;
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Const.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        ActivityService service = retrofit.create(ActivityService.class);
        Call<ActivityDetailBean> call = service.getactivity(aid);
        call.enqueue(new Callback<ActivityDetailBean>() {
            @Override
            public void onResponse(Call<ActivityDetailBean> call, Response<ActivityDetailBean> response) {
                if (response.body() != null){
                    String name = response.body().getA_name();
                    String time = response.body().getA_time();
                    String address = response.body().getA_address();
                    int cost = response.body().getA_money();
                    String pri = String.valueOf(cost);
                    String limit = response.body().getA_most_people();
                    String intro = response.body().getA_intro();
                    String pic = response.body().getA_pic();

                    acTitle.setText(name);
                    acTime.setText(time);
                    acAddress.setText(address);
                    acCost.setText(pri + "元/人");
                    acLimit.setText(limit);
                    acIntro.setText(intro);

                    if (!pic.equals(""))
                        Picasso.with(context)
                                .load("http://10.0.2.2/YueJian16/Public/activityphoto/"+pic)
                                .into(acPic);
                }

            }

            @Override
            public void onFailure(Call<ActivityDetailBean> call, Throwable t) {

            }
        });


    }

    private void acfollow(){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Const.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        FollowService service = retrofit.create(FollowService.class);
        Call<ActivityFollowBean> call = service.acFollow(uid,aid);
        call.enqueue(new Callback<ActivityFollowBean>() {
            @Override
            public void onResponse(Call<ActivityFollowBean> call, Response<ActivityFollowBean> response) {
                if (response.body().isInfo() == true){
                    collectButton.setImageResource(R.drawable.ic_collected);
                    Toast.makeText(getApplicationContext(),"收藏成功",Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFailure(Call<ActivityFollowBean> call, Throwable t) {

            }
        });
    }

    private void accancelfollow(){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Const.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        FollowService service = retrofit.create(FollowService.class);
        Call<ActivityCancelFollowBean> call = service.acnofollow(uid,aid);
        call.enqueue(new Callback<ActivityCancelFollowBean>() {
            @Override
            public void onResponse(Call<ActivityCancelFollowBean> call, Response<ActivityCancelFollowBean> response) {
                if (response.body().isInfo() == true){
                    collectButton.setImageResource(R.drawable.ic_collect);
                    Toast.makeText(getApplicationContext(),"取消收藏成功",Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFailure(Call<ActivityCancelFollowBean> call, Throwable t) {

            }
        });
    }

    private void isfollowed(){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Const.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        FollowService service = retrofit.create(FollowService.class);
        Call<ActivityIsFollowedBean> call = service.isacfollow(uid,aid);
        call.enqueue(new Callback<ActivityIsFollowedBean>() {
            @Override
            public void onResponse(Call<ActivityIsFollowedBean> call, Response<ActivityIsFollowedBean> response) {
                if (response.body().isInfo() == true){
                    collectButton.setImageResource(R.drawable.ic_collected);
                    sta = 1;
                }else {
                    collectButton.setImageResource(R.drawable.ic_collect);
                    sta = 0;
                }
            }

            @Override
            public void onFailure(Call<ActivityIsFollowedBean> call, Throwable t) {

            }
        });
    }



    }

